package de.tum.se.sudoku.gui;

import java.awt.BorderLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import de.tum.se.sudoku.impl.Grid;
import de.tum.se.sudoku.logic.ISolver;
import de.tum.se.sudoku.logic.SolverRandom;
import de.tum.se.sudoku.gui.SolStratComboBox;

public class GUI extends JFrame implements IGui {

	private static final long serialVersionUID = 1L;
	private Grid grid;
	private ISolver solver;

	public static void main(String[] args) {
		Grid grid = new Grid().buildSudoku();
		grid.printGrid();
		new GUI(grid);
	}

	public GUI(Grid grid) {
		createGui(grid);
	}

	public void refreshGui(Grid grid) {
		removeAll();
		createGui(grid);
		this.revalidate();
	}

	private void createGui(Grid grid) {
		this.grid = grid;
		solver = new SolverRandom();

		setLayout(new BorderLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel east = new JPanel();
		east.setLayout(new BoxLayout(east, BoxLayout.PAGE_AXIS));

		JButton solve = new JButton("Solve");
		solve.addActionListener(new SolveButtonActionListener(this));
		east.add(solve, BorderLayout.EAST);

		JButton check = new JButton("Check Solution");
		check.addActionListener(new SolutionButtonActionListener(this));
		east.add(check, BorderLayout.EAST);

		east.add(new SolStratComboBox(this));

		JPanel west = new SudokuGrid(grid);

		this.add(west, BorderLayout.WEST);
		this.add(east, BorderLayout.EAST);
		this.pack();
		this.setVisible(true);
	}

	@Override
	public Grid getGrid() {
		return grid;
	}

	public ISolver getSolver() {
		return solver;
	}

	public void setSolver(ISolver solver) {
		this.solver = solver;
	}
}
